package FactoryMode

import scala.io.StdIn
import util.control.Breaks._

object AbstractFactroy {
  def main(args: Array[String]): Unit = {
    //    val pizza = new BJFactory().createPizza("chesse")
    new OrderPizza(new BJFactory)
  }
}

class orderPizza {

}

abstract trait AbsFactory {
  def createPizza(t: String): Pizza
}

class BJFactory extends AbsFactory {
  override def createPizza(t: String): Pizza = {
    var pizza: Pizza = null
    if (t.equals("chesse"))
      pizza = new BJChees
    else {
      pizza = null
    }
    pizza
  }
}

class BJChees extends Pizza {
  override def prepare(): Unit = ???

  override def bake(): Unit = ???

  override def box(): Unit = ???
}

class OrderPizza {
  var absFactory: AbsFactory = _

  def this(absFactory: AbsFactory) {
    //多态
    this
    breakable {
      var orderType: String = null
      var pizza: Pizza = null
      do {
        println("请输入pizza的类型,使用抽象工厂模式")
        orderType = StdIn.readLine()
        if (pizza == null)
          pizza = absFactory.createPizza(orderType)
        else
          pizza
      } while (true)
    }
  }
}